package com.qdxht.dispenser.ui.activity.test;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.qdxht.dispenser.ui.activity.MqttTestActivity;
import com.qdxht.dispenser.utils.DeviceIdUtil;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.qdxht.dispenser.R;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnPageChangeListener;
import java.util.ArrayList;

public class BannerActivity extends AppCompatActivity {
    private static final long LOOP_TIME = 6000L;
    private int currentPos = 1;
    private Handler taskHandler;
    private PlayAdapter adapter;
    private Banner<PlayResourcesBean, PlayAdapter> banner;
    private ArrayList<PlayResourcesBean> resourcesList;

    public static void start(Context context) {
        Intent intent = new Intent(context, BannerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);
        initData();
        initBanner();

        String androidId = DeviceIdUtil.getAndroidId(this);
        TextView textView = findViewById(R.id.version);
        textView.setText("NO."+androidId.substring(androidId.length() - 4));
    }

    private void initData() {
        taskHandler = new Handler(Looper.getMainLooper());
        resourcesList = new ArrayList<>();
        resourcesList.add(new PlayResourcesBean("file:///android_asset/banner.jpg", true, "图片"));
        resourcesList.add(new PlayResourcesBean("https://upload-images.jianshu.io/upload_images/5809200-a99419bb94924e6d.jpg", true, "图片"));
        resourcesList.add(new PlayResourcesBean("https://s10.mogucdn.com/mlcdn/c45406/170831_479g0ifl6f2i313feb5ech46kek21_778x440.jpg", true, "图片"));
        resourcesList.add(new PlayResourcesBean("https://media.w3.org/2010/05/sintel/trailer.mp4", false, "视频"));
        
        adapter = new PlayAdapter(this, resourcesList, this);
    }

    private void initBanner() {
        banner = findViewById(R.id.banner);
        banner.addBannerLifecycleObserver(this)
                .setAdapter(adapter, true)
                .setLoopTime(LOOP_TIME);

        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == banner.getRealCount() - 1) {
                    currentPos = banner.getRealCount();
                } else if (position == 0) {
                    currentPos = 1;
                } else {
                    currentPos = position + 1;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager2.SCROLL_STATE_IDLE) {
                    if (banner.isInfiniteLoop()) {
                        taskHandler.post(task);
                    }
                }
            }
        });
    }

    private final Runnable task = new Runnable() {
        @Override
        public void run() {
            if (adapter.getVHMap().containsKey(currentPos)) {
                if (adapter.getVHMap().get(currentPos) instanceof PlayAdapter.VideoHolder) {
                    PlayAdapter.VideoHolder holder = (PlayAdapter.VideoHolder) adapter.getVHMap().get(currentPos);
                    GSYVideoManager.onPause();
                    holder.video.startPlayLogic();
                    banner.isAutoLoop(true);
                    banner.stop();
                    banner.isAutoLoop(false);
                } else {
                    GSYVideoManager.onPause();
                    banner.isAutoLoop(true);
                    banner.start();
                }
            } else {
                GSYVideoManager.onPause();
                banner.isAutoLoop(true);
                banner.start();
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        taskHandler.post(task);
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        banner.destroy();
    }

    public Banner<PlayResourcesBean, PlayAdapter> getBanner() {
        return banner;
    }

    public long getLoopTime() {
        return LOOP_TIME;
    }
} 